#include "bend2d.h"
#include "glutproject.h"
#include "battlefield.h"
#include "customobjectcreator.h"
#include "factory.h"
#include "tank.h"
using namespace bend2d;

class myGlutProject : public GlutProject
{
    public:
        myGlutProject() : m_BattleField(NULL) {}
        void CustomInit()
        {
            Factory::GetInstance()->RegistryObjectCreator(new CustomObjectCreator);
            m_BattleField = new BattleField(bend2d::Rectangle(Point(400,300), Angle(0), 800, 600));
            GlutProject::CustomInit();
            
            myTank = new Tank(Point(400,300));
            myTank->GetTankColor() = Color(0x008000FF);
            myTank->SetAngle(Angle(45));
            
            m_BattleField->RegistryObject(myTank);

       }
        
        void ClearFunction()
        {
            glClearColor(1, 1, 1, 0);
            glClear(GL_COLOR_BUFFER_BIT);             
        }
        
        void GetWindowSize(int& width, int& height)
        {
            width  = 800;
            height = 600;
        }
        
        void GetWindowName(string& windowname)
        {
            windowname = "Tank Warfare";
        }
        
        void CustomRenderFunction()
        {
            m_BattleField->Draw();
        }
        
        void CustomIdleFunction()
        {
             m_BattleField->Iterate(m_BattleField->GetTimeSinceLastIteration());
             m_BattleField->Iterated();
             this->RenderFunction();
        }

        void CustomKeyboardFunction(unsigned char key, int x, int y)
        {
            switch(key)
            {
            case 'a':
                myTank->GetTankFireEngine()->SetAngle(myTank->GetTankFireEngine()->GetAngle().Rotate(2));
                break;
            case 'd':
                myTank->GetTankFireEngine()->SetAngle(myTank->GetTankFireEngine()->GetAngle().Rotate(-2));
                break;
            case '0':
                myTank->GetTankFireEngine()->Shot(m_BattleField);
                break;

                    

            }
        }
        
    private:
        BattleField* m_BattleField;
        Tank* myTank;
};

int main(int argc, char* argv[])
{
    myGlutProject project;
    project.Init(argc, argv);
    project.MainLoop();

    return 0;
}
